package com.dtjd.playcoinmonkey.activities;

import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.request.RequestOptions;
import com.dtjd.playcoinmonkey.R;
import com.dtjd.playcoinmonkey.bean.HttpParaBean;
import com.dtjd.playcoinmonkey.databinding.ActivityAddTicketBinding;
import com.dtjd.playcoinmonkey.utils.Network;
import com.dtjd.playcoinmonkey.utils.Tools;

import org.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class AddTicketActivity extends AppCompatActivity {

    private ActivityAddTicketBinding binding;
    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        if (android.R.id.home==item.getItemId()){ finish(); }
        return super.onOptionsItemSelected(item);
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding= ActivityAddTicketBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        ActionBar actionbar = getSupportActionBar();
        if (actionbar!=null){
            actionbar.setDisplayHomeAsUpEnabled(true);
            actionbar.setHomeButtonEnabled(true);
        }
        setTitle(getResources().getString(R.string.add_ticket));
        binding.addTicketUser.setVisibility(View.GONE);
        binding.addTicketNum.setOnFocusChangeListener((view, b) -> {
            if (b){
                List<HttpParaBean> para=new ArrayList<>();
                para.add(new HttpParaBean("email",binding.addTicketMail.getText().toString()));
                Network.post(para, "wbhMain/getUserInfo", new Callback() {
                    @Override
                    public void onFailure(@NonNull Call call, @NonNull IOException e) {
                        e.printStackTrace();
                    }
                    @Override
                    public void onResponse(@NonNull Call call, @NonNull Response response) {
                        runOnUiThread(() -> {
                            try {
                                JSONObject jo = new JSONObject(Objects.requireNonNull(response.body()).string());
                                if (jo.has("datalist")){
                                    JSONObject datalist = jo.getJSONObject("datalist");
                                    binding.addTicketUser.setVisibility(View.VISIBLE);
                                    binding.addTicketNickname.setText(datalist.getString("wb_nickname"));
                                    Glide.with(AddTicketActivity.this).load(datalist.getString("wb_url")).apply(RequestOptions.bitmapTransform(new CircleCrop())).placeholder(R.drawable.loading).placeholder(R.drawable.loading).error(R.drawable.loaderr).into(binding.addTicketIcon);
                                }else {
                                    Tools.toast(getResources().getString(R.string.userNotExist));
                                }
                            }catch (Exception e){
                                e.printStackTrace();
                            }
                        });
                    }
                });

            }else{
                binding.addTicketUser.setVisibility(View.GONE);
            }
        });

        binding.addTicketAdd.setOnClickListener(view -> {
            if (binding.addTicketMail.getText().toString().equals("")){
                Tools.toast(getResources().getString(R.string.mail_cant_blank));
                return;
            }
            if (binding.addTicketNum.getText().toString().equals("")){
                Tools.toast(getResources().getString(R.string.input_num));
                return;
            }
            List<HttpParaBean> para=new ArrayList<>();
            para.add(new HttpParaBean("ticket",binding.addTicketNum.getText().toString()));
            para.add(new HttpParaBean("email",binding.addTicketMail.getText().toString()));
            para.add(new HttpParaBean("token","token"));
            Network.post(para, "admin/addTicketToUser", new Callback() {
                @Override
                public void onFailure(@NonNull Call call, @NonNull IOException e) {
                    e.printStackTrace();
                }
                @Override
                public void onResponse(@NonNull Call call, @NonNull Response response) {
                runOnUiThread(() -> {
                    try {
                        Tools.toast(new JSONObject(response.body().string()).getString("datalist"));
                        AddTicketActivity.this.finish();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                });
                }
            });


        });
    }
}